from datetime import date
from typing import List

from django.shortcuts import get_object_or_404
from pydantic import BaseModel
from flask_openapi3 import APIBlueprint
from django.core.serializers import serialize
from .models import Event

router = APIBlueprint('demo_api', __name__, url_prefix="/demo_api")


class EventSchema(BaseModel):
    title: str
    start_date: date
    end_date: date

    class Config:
        from_attributes = True


class IDSchema(BaseModel):
    id: int


@router.post("/create")
def create_event(body: EventSchema):
    Event.objects.create(**body.model_dump())
    return body.model_dump()


@router.get("")
def list_events():
    data = serialize("python", Event.objects.all(), indent=4,
                     use_natural_foreign_keys=True, use_natural_primary_keys=True)
    return data


@router.delete("")
def delete_events():
    Event.objects.all().delete()
    return


@router.get("/<int:id>")
def get_event(path: IDSchema):
    event = get_object_or_404(Event, id=path.id)
    return serialize("python", event, indent=4,
                     use_natural_foreign_keys=True, use_natural_primary_keys=True)
